package com.wufun.overseas.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wufun.baselibrary.base.BaseAppCompatActivity;
import com.wufun.overseas.R;
import com.wufun.overseas.joystick.map.KeyMapManager;
import com.wufun.overseas.models.bean.IntentDateBean;
import com.wufun.overseas.utils.IntentUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 手柄按键映射界面
 * Created by huzy on 2017/3/17.
 */
public class HandShankMapKeyActivity extends BaseAppCompatActivity implements View.OnClickListener {

    private ImageView on_off_img;
    private ImageView handshank_on_off;
    private boolean isHide = true;
    @Bind(R.id.rightButn)
    Button   rightButn;
    @Bind(R.id.titleText)
    TextView titleText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handshank_mapkey);
        ButterKnife.bind(this);

        afterView();

    }

    //    @AfterViews
    void afterView() {
        titleText.setText(getString(R.string.gamepad_keymap));
        rightButn.setVisibility(View.GONE);
        handshank_on_off = (ImageView) findViewById(R.id.handshank_on_off);
        on_off_img = (ImageView) findViewById(R.id.on_off_img);
        setVirtualHide(KeyMapManager.virtualKeyboard(0));
        findViewById(R.id.lay_1).setOnClickListener(this);
        findViewById(R.id.lay_2).setOnClickListener(this);

        LinearLayout fba_lay = (LinearLayout) findViewById(R.id.fba_lay);
        LinearLayout gba_lay = (LinearLayout) findViewById(R.id.gba_lay);
        LinearLayout fc_lay = (LinearLayout) findViewById(R.id.fc_lay);
        LinearLayout sfc_lay = (LinearLayout) findViewById(R.id.sfc_lay);
        LinearLayout psp_lay = (LinearLayout) findViewById(R.id.psp_lay);
        LinearLayout md_lay = (LinearLayout) findViewById(R.id.md_lay);
        LinearLayout ps_lay = (LinearLayout) findViewById(R.id.ps_lay);
        LinearLayout wsc_lay = (LinearLayout) findViewById(R.id.wsc_lay);
        LinearLayout gbc_lay = (LinearLayout) findViewById(R.id.gbc_lay);
        LinearLayout nds_lay = (LinearLayout) findViewById(R.id.nds_lay);

        String appFlag = getResources().getString(R.string.app_flag);
        if (appFlag.equals("APP_ARC")) {
            fba_lay.setVisibility(View.VISIBLE);
            fba_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_GBA")) {
            gba_lay.setVisibility(View.VISIBLE);
            gba_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_NES")) {
            fc_lay.setVisibility(View.VISIBLE);
            fc_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_SNES")) {
            sfc_lay.setVisibility(View.VISIBLE);
            sfc_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_PSP")) {
            psp_lay.setVisibility(View.VISIBLE);
            psp_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_PS")) {
            ps_lay.setVisibility(View.VISIBLE);
            ps_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_WSC")) {
            wsc_lay.setVisibility(View.VISIBLE);
            wsc_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_GBC")) {
            gbc_lay.setVisibility(View.VISIBLE);
            gbc_lay.setOnClickListener(this);
        } else if (appFlag.equals("APP_NDS")) {
            nds_lay.setVisibility(View.VISIBLE);
            nds_lay.setOnClickListener(this);
        }

        findViewById(R.id.papaHandList).setOnClickListener(this);
    }

    @OnClick(R.id.backImage)
    void backImage() {
        finish();
    }

    private IntentDateBean createnIntent(int type, String url) {
        IntentDateBean intentDateBean = new IntentDateBean();
        intentDateBean.setLink_type(type);
        intentDateBean.setLink_type_val(url);
        intentDateBean.setObject(getString(R.string.gamepad_nds_settings));
        return intentDateBean;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.papaHandList || id == R.id.nds_lay) {
            IntentDateBean intentDateBean = null;
            if (id == R.id.papaHandList) {
                intentDateBean = createnIntent(4, "http://h5.papa91.com/wf_shoubing.html");
            } else if (id == R.id.nds_lay) {
                intentDateBean = createnIntent(4, "http://apphd.papa91.com/nds/index.html");
            }
            IntentUtil.getInstance().goShareWebActivity(this, intentDateBean);
            return;
        }

        if (id == R.id.lay_1) {
            setVirtualHide(!isHide);
        } else if (id == R.id.lay_2) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankCheckKeyActivity.class));
        } else if (id == R.id.fba_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankFBAActivity.class));
        } else if (id == R.id.gba_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankGBAActivity.class));
        } else if (id == R.id.fc_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankFCActivity.class));
        } else if (id == R.id.sfc_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankSFCActivity.class));
        } else if (id == R.id.psp_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankPSPActivity.class));
        } else if (id == R.id.md_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankMDActivity.class));
        } else if (id == R.id.ps_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankPSActivity.class));
        } else if (id == R.id.wsc_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankWSCActivity.class));
        } else if (id == R.id.gbc_lay) {
            startActivity(new Intent(HandShankMapKeyActivity.this, HandShankGBCActivity.class));
        }
    }

    private void setVirtualHide(boolean hide) {
        if (!hide) {
            handshank_on_off.setImageResource(R.drawable.handshank_virtualkey_off);
            on_off_img.setImageResource(R.drawable.handshank_virtualkey_on_icon);
        } else {
            handshank_on_off.setImageResource(R.drawable.handshank_virtualkey_on);
            on_off_img.setImageResource(R.drawable.handshank_virtualkey_off_icon);
        }
        if (isHide != hide) {
            isHide = hide;
            KeyMapManager.virtualKeyboard(0, isHide);
        }
    }
}
